package com.pro.springbootshop.controller;

import com.pro.springbootshop.entity.FlashActivity;
import com.pro.springbootshop.entityExtend.FlashActivityReturn;
import com.pro.springbootshop.mapper.FlashActivityMapper;
import com.pro.springbootshop.service.FlashActivityService;
import com.pro.springbootshop.util.TokenUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class FlashActivityController {
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    FlashActivityService flashActivityService;
    @Autowired
    FlashActivityMapper flashActivityMapper;
    @ApiOperation("提交秒杀活动")
    @PostMapping("/api/flashActivity/insert")
    public String saveFlashActivity(@RequestBody FlashActivity flashActivity){

        return flashActivityService.saveFlashActivity(flashActivity);
    }
    @ApiOperation("管理员获取所有秒杀活动")
    @GetMapping("/api/flashActivity/{page}/{limit}")
    public FlashActivityReturn getAllFlashActivity(@PathVariable(name = "page") Integer page, @PathVariable(name = "limit") Integer limit){
        return flashActivityService.getAllFlashActivity(page,limit);
    }
    @ApiOperation("修改秒杀活动状态")
    @GetMapping("/api/flashActivity/change/{flashActivityId}/{flashActivityState}")
    public String changeFlashActivity(@PathVariable(name = "flashActivityId") Integer flashActivityId, @PathVariable(name = "flashActivityState") Integer flashActivityState){
        return flashActivityService.changeFlashActivity(flashActivityId,flashActivityState);
    }
    @ApiOperation("商品秒杀是否可以秒杀和自动商品数量减少")
    @GetMapping("/api/flashActivitywaresID/change/{flashActivityId}")
    public String changeFlashActivityWaresNumber(@PathVariable(name = "flashActivityId") Integer flashActivityId){
        FlashActivity flashActivity=flashActivityMapper.getFlashActivityById(flashActivityId);
        if (flashActivity.getWaresNumber()>0)
        {
        String mes=flashActivityService.changeFlashActivityWaresNumber(flashActivityId,flashActivity.getWaresNumber()-1);
        return "商品抢购成功";
        }
        else{
            return "商品已抢购完";
        }
    }
    @ApiOperation("商家获取自己的秒杀活动")
    @GetMapping("/api/buyer/flashActivity/{page}/{limit}/{sellerId}")
    public FlashActivityReturn sellerGetAllFlashActivity(@PathVariable(name = "page") Integer page, @PathVariable(name = "limit") Integer limit,@PathVariable(name = "sellerId") String sellerId){
        return flashActivityService.sellerGetAllFlashActivity(page,limit,sellerId);
    }
//    @ApiOperation("根据商品ID获取有关的的秒杀活动")
//    @GetMapping("/api/flashActivity/{waresId}")
//    public FlashActivityReturn buyerGetFlashActivityByWaresID(@PathVariable(name = "waresId") Integer waresId){
//        return flashActivityService.buyerGetFlashActivityByWaresID(waresId);
//    }

}